package vg.services.log_manager.realization;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import vg.services.log_manager.interfaces.IWindowMessage;
import vg.services.main_manager.MainManager;

/**
 * This class contains methods for output of
 * different information on the window.
 * @author tzolotuhin
 */
public class WindowMessage implements IWindowMessage {
	/**
	 * This method shows window with error. 
	 */
	public void errorMessage(final String text, final String title) {
		MainManager.log.printError(text);
		if (SwingUtilities.isEventDispatchThread()) {
			String msg = "";
			if(text != null) {
				msg = text + "\n\nAdditional information : \n" + MainManager.log.getLoggerInfo();
			} else {
				msg = "Additional information : \n" + MainManager.log.getLoggerInfo();
			}
			Object[] options = {"Close"};
			JOptionPane.showOptionDialog(null,msg,title,JOptionPane.CANCEL_OPTION,JOptionPane.ERROR_MESSAGE,null,options,options[0]);
		} else {
			SwingUtilities.invokeLater(new Runnable() {			
				public void run() {
					errorMessage(text, title);
				}
			});
		}
	}
	/**
	 * This method shows window with warning. 
	 */
	public void warningMessage(final String text, final String title) {
		MainManager.log.printError(text);
		if (SwingUtilities.isEventDispatchThread()) {
			String msg = "";
			if(text != null) {
				msg = text + "\n\nAdditional information : \n" + MainManager.log.getLoggerInfo();
			} else {
				msg = "Additional information : \n" + MainManager.log.getLoggerInfo();
			}
			Object[] options = {"Close"};
			JOptionPane.showOptionDialog(null,msg,title,JOptionPane.CANCEL_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);
		} else {
			SwingUtilities.invokeLater(new Runnable() {			
				public void run() {
					warningMessage(text, title);
				}
			});
		}
	}
	/**
	 * This method shows window with information. 
	 */
	public void infoMessage(final String text, final String title) {
		MainManager.log.printInfo(text);
		if (SwingUtilities.isEventDispatchThread()) {
			Object[] options = {"Close"};
			JOptionPane.showOptionDialog(null,text,title,JOptionPane.CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE,null,options,options[0]);
		} else {
			SwingUtilities.invokeLater(new Runnable() {			
				public void run() {
					infoMessage(text, title);
				}
			});
		}
	}
	/**
	 * This method show window with error information
	 */
	public void showExceptionMessage(final Exception e)
	{
		MainManager.log.printException(e);
		if (SwingUtilities.isEventDispatchThread()) {
			errorMessage(e.getMessage(), "Error");
		} else {
			SwingUtilities.invokeLater(new Runnable() {			
				public void run() {
					showExceptionMessage(e);
				}
			});
		}
			
	}
}
